from django.contrib.auth.models import User, Group
# from rest_framework import viewsets
# from blog.serializers import UserSerializer, GroupSerializer
from django.http import HttpResponse, QueryDict
from blog.models import *
import os

def index(req):
    return HttpResponse('blog-index')


def news(req, pk=None):
    if req.method == 'GET':
        # news = News.objects.all()
        news = News.objects.filter(status=1)[2:7]
        # print(news)



        print(os.getenv('MYSQL_USER'))

        # print(os.getenv('MYSQL_USER'))

        return HttpResponse('list')

    elif req.method == 'POST':
        name =  req.POST['name']
        body =  req.POST['body']
        status =  req.POST['status']
        news = News(title=name, timestamp='2018-10-30', body=body, status=status)
        news.save()


    return HttpResponse('test')


def newsDetail(req, pk):
    if req.method == 'GET':
        # news = News.objects.filter(pk=pk).first()
        news = News.objects.get(pk=pk)
        print(news)

    elif req.method == 'PUT':
        news = News.objects.get(pk=pk)
        put = QueryDict(req.body)
        news.title = put.get('name')
        news.body = put.get('body')
        news.status = put.get('status')
    elif req.method == 'DELETE':
        news = News.objects.get(pk=pk)
        news.delete()

    return HttpResponse('new Detail')